package com.fh.qqserver.service;

import com.fh.qqcommon.Message;
import com.fh.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @author 方小勤
 * @version 1.0~
 * 该类的一个对象和某个客户端保持通讯
 */
public class ServerConnectClientThread extends Thread {
    private Socket socket;
    private String userId;//连接到服务端的用户id

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }
    public Socket getSocket(){
        return socket;
    }
    @Override
    public void run() {
        while (true) {
            System.out.println("服务端和客户端 "+ userId + " 保持通讯,读取数据。。。");
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) objectInputStream.readObject();
                //根据message的类型，做相应的业务处理
                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)){
                    //客户端要在线用户列表
                    System.out.println(message.getSender()+" 要在线用户列表");
                    String onlineUser = ManageClientThreads.getOnlineUser();
                    //返回message
                    Message message1 = new Message();
                    message1.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    message1.setContent(onlineUser);
                    message1.setReceiver(message.getSender());
                    //写入到数据通道，返回给客户端
                    new ObjectOutputStream(socket.getOutputStream()).writeObject(message1);
                }else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES_FRIEND)){
                    //根据 message 获取 getter id，然后再得到对应线程
                    ServerConnectClientThread serverConnectClientThread = ManageClientThreads.getServerConnectClientThread(message.getReceiver());
                    new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream()).writeObject(message);//转发，如果客户不在线，可以保存到数据库，可以实现离线留言的功能
                } else if(message.getMesType().equals(MessageType.MESSAGE_TO_ALL_FRIEND)){
                    //需要遍历 管理线程集合，将所有线程的socket得到，然后将message转发
                    HashMap<String,ServerConnectClientThread> hm = ManageClientThreads.getServerConnectClientThreadHashMap();
                    Iterator<String> iterator = hm.keySet().iterator();
                    while (iterator.hasNext()){
                        //取出在线用户id
                        String onlineUserId = iterator.next().toString();
                        if (!onlineUserId.equals(message.getSender())){//排除群发消息的用户
                            //进行转发message
                            new ObjectOutputStream(hm.get(onlineUserId).getSocket().getOutputStream()).writeObject(message);
                        }
                    }
                }
                else if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                    System.out.println(message.getSender() + " 退出");
                    //将这个客户端对应的线程，从集合中删除
                    ManageClientThreads.removeClientThread(message.getSender());
                    socket.close();//关闭连接
                    //退出线程
                    break;
                }
                else{
                    System.out.println("其他类型占时不处理");

                }
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
